package org.example;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class Crawler {

    public static void main(String[] args) throws IOException {
        crawlByBid();
    }


    public static void crawlByBid() throws IOException {
        URL url = new URL("https://www.dygang.tv/ys/");
        Document doc = Jsoup.parse(url, 5000);
        Elements elements = doc.select("table.border1 a");
        for (Element element : elements) {
            String href = element.attr("href");
            String vid = href.split("/")[2] + "/" + href.split("/")[3];
            System.out.println(vid);
            crawlByBook(vid);
        }


    }

    public static void crawlByBook(String vid) throws IOException {
        URL url = new URL("https://www.dygang.tv/ys/"+vid);
        Document doc = Jsoup.parse(url, 5000);
        Map<String, Object> vedio = new HashMap<>();
        String backGround = doc.select("td#dede_content>p>img").attr("src");
        String image = doc.select("td#dede_content>p>br+img").attr("src");
        String detail = doc.selectFirst("td#dede_content p").text().trim();
        String[] details = detail.split("[\\s\\n/]+");
        for (String s : details) {
            System.out.println(s);
        }
        vedio.put("backGround", backGround);
        vedio.put("image", image);
        vedio.put("name", details[0]);
        if (details.length > 3) {
            vedio.put("time", details[2]);
        }


        if (details.length > 4) {
            vedio.put("country", details[3]);
        }
        if (details.length > 5) {
            vedio.put("category", details[4]);
        }
        if (details.length > 6) {
            vedio.put("lang", details[5]);
        }
        if (details.length > 7) {
            vedio.put("lookTime", details[6]);
        }
        if (details.length > 8) {
            vedio.put("score", details[7]);
        }
        if (details.length > 10) {
            vedio.put("longTime", details[9]);
        }

        if (details.length > 11) {
            vedio.put("directors", details[10]);
        }
        if (details.length > 12) {
            vedio.put("actor", details[11]);        }

        System.out.println(vedio);
    }
}
